﻿// (c) Copyright Microsoft Corporation.
// This source is subject to the Microsoft Public License (Ms-PL).
// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.
// All other rights reserved.

using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media.Animation;

namespace Coding4Fun.Chair.Cockpit.Controls
{
	/// <summary>
	/// Interaction logic for NumericStream.xaml
	/// </summary>
	public partial class NumericStream : UserControl
	{
        private readonly Random _random = new Random();

		public Storyboard uxSBGameLoop;

	    readonly List<StackPanel> _panels = new List<StackPanel>();
	    readonly List<List<TextBlock>> _textBlocks = new List<List<TextBlock>>();

        public NumericStream()
        {
            InitializeComponent();

            Loaded += NumericStreamLeft_Loaded;
        }

        void NumericStreamLeft_Loaded(object sender, RoutedEventArgs e)
        {
            uxSBGameLoop = FindResource("uxSBGameLoop") as Storyboard;

            if (uxSBGameLoop != null)
            {
                uxSBGameLoop.Completed += uxSBGameLoop_Completed;
                uxSBGameLoop.Begin();
            }

            for (var i = 0; i < 4; i++)
            {
                var panel = FindName(String.Format("uxTH{0}", i)) as StackPanel;

                if (panel != null)
                    _panels.Add(panel);

                _textBlocks.Add(new List<TextBlock>());

                for (var j = 0; j < 3; j++)
                {
                    var textBlock = FindName(String.Format("uxT{0}_{1}", i, j)) as TextBlock;

                    if (textBlock != null)
                        _textBlocks[i].Add(textBlock);
                }
            }
        }

		/// <summary>
		/// Index refers to the line, and number is the display value.
		/// </summary>
        public void SetValue(int index, double number)
		{
		    _textBlocks[index].ForEach(t =>
		                                   {
		                                       t.Text = number.ToString();
		                                   });
		}

	    void uxSBGameLoop_Completed(object sender, EventArgs e)
		{
			for (int index = 0; index <= 3; index++)
			{
                _textBlocks[index].ForEach(t =>
                {
                    t.Visibility = Visibility.Collapsed;
                });
				
				var randomNumber = _random.Next(3);
                _textBlocks[index][randomNumber].Visibility = Visibility.Visible;

                HorizontalAlignment alignment;

				switch (randomNumber)
				{
					case 0:
						alignment = HorizontalAlignment.Left;
						break;
					
					case 2:
						alignment = HorizontalAlignment.Right;
						break;

                    default:
                        alignment = HorizontalAlignment.Center;
                        break;
				}

                _panels[index].HorizontalAlignment = alignment;
			}

			uxSBGameLoop.Begin();
		}
	}
}